package net.caixiaomi.info.ui.dialog;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.BaseDialogFragment;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.MatchInfoForTeam;
import net.caixiaomi.info.model.MatchIntroduceModel;
import net.caixiaomi.info.model.MatchTeamInfo;
import net.caixiaomi.info.model.MatchTeamSupport;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.ui.football.detail.MatchDetailActivity;
import net.caixiaomi.info.util.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * 比赛介绍
 */

public class FootballMatchIntroduceDialogFragment extends BaseDialogFragment {

    @BindView(R.id.time)
    TextView mTime;
    @BindView(R.id.icon_host)
    ImageView mIconHost;
    @BindView(R.id.host)
    TextView mHost;
    @BindView(R.id.tie)
    TextView mTie;
    @BindView(R.id.icon_guest)
    ImageView mIconGuest;
    @BindView(R.id.guest)
    TextView mGuest;
    @BindView(R.id.status)
    TextView mStatus;
    @BindView(R.id.host_host)
    TextView mHostHost;
    @BindView(R.id.guest_guest)
    TextView mGuestGuest;
    @BindView(R.id.battle)
    TextView mBattle;
    @BindView(R.id.victory1)
    TextView mVictory1;
    @BindView(R.id.tie1)
    TextView mTie1;
    @BindView(R.id.guest1)
    TextView mGuest1;
    @BindView(R.id.tip)
    TextView mTip;
    @BindView(R.id.victory2)
    TextView mVictory2;
    @BindView(R.id.tie2)
    TextView mTie2;
    @BindView(R.id.guest2)
    TextView mGuest2;

    @OnClick(android.R.id.button1)
    void toLookDetail() {
        Intent intent = new Intent(getActivity(), MatchDetailActivity.class);
        intent.putExtra("data", mMatchId);
        startActivity(intent);
        dismissAllowingStateLoss();
    }

    @OnClick(android.R.id.button2)
    void toCancel() {
        dismissAllowingStateLoss();
    }


    private String mMatchId = "";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mMatchId = getArguments().getString("data");
        loadData();
    }

    @Override
    protected int layoutRes() {
        return net.caixiaomi.info.R.layout.dialog_match_intorduce;
    }


    @Override
    protected void onViewCreated(@NonNull View view) {
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wlp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        wlp.gravity = Gravity.BOTTOM;
        window.setAttributes(wlp);
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        window.setBackgroundDrawable(new ColorDrawable(0xff000000));
        getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
    }

    private void loadData() {
        JSONObject params = new JSONObject();
        params.put("matchId", mMatchId);
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel<MatchIntroduceModel>> baseCallModelCall = requestService.matchTeamInfosSum(params);
        baseCallModelCall.enqueue(new ResponseCallback<BaseCallModel<MatchIntroduceModel>>(getActivity()) {
            @Override
            public void onSuccess(BaseCallModel<MatchIntroduceModel> data) {
                MatchIntroduceModel model = data.data;
                bindData(model);
            }


            @Override
            public void onFail(ResponseError error) {
                if (error.getCode() >= 300000 && error.getCode() <= 310000) {
                    ToastUtil.s(error.getMessage());
                }
            }

            @Override
            public void onError() {

            }
        });

    }

    private void bindData(MatchIntroduceModel model) {
        try {
            if (model != null) {
                StringBuilder stringBuilder = new StringBuilder();
                MatchInfoForTeam matchInfo = model.getMatchInfo();
                RequestOptions options = new RequestOptions();
                options.error(R.mipmap.ic_football);
                options.placeholder(R.mipmap.ic_football);
                Glide.with(CommonApp.getInstance()).load(matchInfo.getHomeTeamPic()).apply(options).into(mIconHost);
                Glide.with(CommonApp.getInstance()).load(matchInfo.getVisitingTeamPic()).apply(options).into(mIconGuest);
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                String time = sdf.format(new Date(Long.parseLong(matchInfo.getMatchTime()) * 1000));
                stringBuilder.append(matchInfo.getChangci()).append("  ").append(matchInfo.getLeagueAddr()).append("  ").append(time);
                mTime.setText(stringBuilder);

                stringBuilder.setLength(0);
                if (!TextUtils.isEmpty(matchInfo.getHomeTeamRank())) {
                    stringBuilder.append("[").append(matchInfo.getHomeTeamRank()).append("]");
                }
                stringBuilder.append(matchInfo.getHomeTeamAbbr());
                String homeTeam = stringBuilder.toString();
                stringBuilder.setLength(0);
                stringBuilder.append(homeTeam).append("\n").append(getString(net.caixiaomi.info.R.string.C_HOST_WIN)).append(matchInfo.getHOdds());
                SpannableString spannableString = new SpannableString(stringBuilder);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.third_text)), homeTeam.length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new RelativeSizeSpan(0.8f), homeTeam.length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                mHost.setText(spannableString);

                stringBuilder.setLength(0);
                stringBuilder.append("VS").append("\n").append(getString(net.caixiaomi.info.R.string.C_DRAW)).append(matchInfo.getDOdds());
                spannableString = new SpannableString(stringBuilder);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.third_text)), 3, stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new RelativeSizeSpan(0.8f), 3, stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                mTie.setText(spannableString);

                stringBuilder.setLength(0);
                if (!TextUtils.isEmpty(matchInfo.getVisitingTeamRank())) {
                    stringBuilder.append("[").append(matchInfo.getVisitingTeamRank()).append("]");
                }

                stringBuilder.append(matchInfo.getVisitingTeamAbbr());
                String visitTeam = stringBuilder.toString();
                stringBuilder.setLength(0);
                stringBuilder.append(visitTeam).append("\n").append(getString(net.caixiaomi.info.R.string.C_GUEST_WIN)).append(matchInfo.getAOdds());
                spannableString = new SpannableString(stringBuilder);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.third_text)), visitTeam.length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new RelativeSizeSpan(0.8f), visitTeam.length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                mGuest.setText(spannableString);

                MatchTeamInfo hMatchTeamInfo = model.gethMatchTeamInfo();//近期状态
                MatchTeamInfo vMatchTeamInfo = model.getvMatchTeamInfo();//近期状态
                stringBuilder.setLength(0);
                stringBuilder.append(getString(net.caixiaomi.info.R.string.C_HOST)).append(hMatchTeamInfo.getWin()).append(getString(net.caixiaomi.info.R.string.C_WIN)).append(hMatchTeamInfo.getDraw()).append(getString(net.caixiaomi.info.R.string.C_DRAW)).append(hMatchTeamInfo.getLose()).append(getString(net.caixiaomi.info.R.string.C_DEFEAT));
                stringBuilder.append("  ");
                stringBuilder.append(getString(net.caixiaomi.info.R.string.C_GUEST)).append(vMatchTeamInfo.getWin()).append(getString(net.caixiaomi.info.R.string.C_WIN)).append(vMatchTeamInfo.getDraw()).append(getString(net.caixiaomi.info.R.string.C_DRAW)).append(vMatchTeamInfo.getLose()).append(getString(net.caixiaomi.info.R.string.C_DEFEAT));

                mStatus.setText(stringBuilder);
                stringBuilder.setLength(0);

                MatchTeamInfo hhMatchTeamInfo = model.getHhMatchTeamInfo();//主队主场
                stringBuilder.append(getString(net.caixiaomi.info.R.string.C_HOST)).append(hhMatchTeamInfo.getWin()).append(getString(net.caixiaomi.info.R.string.C_WIN)).append(hhMatchTeamInfo.getDraw()).append(getString(net.caixiaomi.info.R.string.C_DRAW)).append(hhMatchTeamInfo.getLose()).append(getString(net.caixiaomi.info.R.string.C_DEFEAT));
                mHostHost.setText(stringBuilder);

                stringBuilder.setLength(0);
                MatchTeamInfo vvMatchTeamInfo = model.getVvMatchTeamInfo();//客队客场
                stringBuilder.append(getString(net.caixiaomi.info.R.string.C_GUEST)).append(vvMatchTeamInfo.getWin()).append(getString(net.caixiaomi.info.R.string.C_WIN)).append(vvMatchTeamInfo.getDraw()).append(getString(net.caixiaomi.info.R.string.C_DRAW)).append(vvMatchTeamInfo.getLose()).append(getString(net.caixiaomi.info.R.string.C_DEFEAT));
                mGuestGuest.setText(stringBuilder);
                stringBuilder.setLength(0);
                MatchTeamInfo hvMatchTeamInfo = model.getHvMatchTeamInfo();
                String hostWin = stringBuilder.append(hvMatchTeamInfo.getWin()).append(getString(net.caixiaomi.info.R.string.C_WIN)).toString();
                stringBuilder.setLength(0);
                String hostDraw = stringBuilder.append(hvMatchTeamInfo.getDraw()).append(getString(net.caixiaomi.info.R.string.C_DRAW)).toString();
                stringBuilder.setLength(0);
                String hostDefeat = stringBuilder.append(hvMatchTeamInfo.getLose()).append(getString(net.caixiaomi.info.R.string.C_DEFEAT)).toString();
                stringBuilder.setLength(0);

                stringBuilder.append(getString(net.caixiaomi.info.R.string.C_BATTLE_COUNT, hvMatchTeamInfo.getTotal())).append(" ").append(getString(net.caixiaomi.info.R.string.C_HOST)).append(" ").append(hostWin).append(" ").append(hostDraw).append(" ").append(hostDefeat);
                spannableString = new SpannableString(stringBuilder);
                int start = stringBuilder.indexOf(hostWin);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), net.caixiaomi.info.R.color.orange_primary)), start, start + hostWin.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                start = stringBuilder.indexOf(hostDraw);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), net.caixiaomi.info.R.color.blue)), start, start + hostDraw.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                start = stringBuilder.indexOf(hostDefeat);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), net.caixiaomi.info.R.color.green)), start, start + hostDefeat.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                mBattle.setText(spannableString);

                MatchTeamSupport hadTeamSupport = model.getHadTeamSupport();//胜平负支持率
                mVictory1.setText(hadTeamSupport.getHSupport());
                mTie1.setText(hadTeamSupport.getDSupport());
                mGuest1.setText(hadTeamSupport.getASupport());

                MatchTeamSupport hhadTeamSupport = model.getHhadTeamSupport();//让球胜平负支持率
                mVictory2.setText(hhadTeamSupport.getHSupport());
                mTie2.setText(hhadTeamSupport.getDSupport());
                mGuest2.setText(hhadTeamSupport.getASupport());

                String fixedOdds = hhadTeamSupport.getFixedOdds();//+1 、-1
                mTip.setText(fixedOdds);
                mTip.setBackgroundColor(ContextCompat.getColor(CommonApp.getInstance(), fixedOdds.startsWith("+") ? R.color.orange_primary : R.color.green));

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
